﻿using System;
using System.Web;
using System.Web.Caching;
using System.Runtime.Caching;

namespace WS.EKA.Utilities
{
    public class CacheManager
    {
        private static readonly CacheManager instance = new CacheManager();
        private static readonly ObjectCache memoryCache = MemoryCache.Default;
        private CacheManager()
        {
        }
        public static CacheManager Instance
        {
            get { return instance; }
        }

        public void Insert(string key, object obj)
        {
            Cache cache = HttpContext.Current.Cache;
            DateTime timeForExpired = DateTime.Now.AddHours(CacheExpiredHour);
            cache.Insert(key, obj, null, timeForExpired, Cache.NoSlidingExpiration);
        }
        public void Insert(string key, object obj, DateTime timeForExpired, CacheDependency dependencies)
        {
            Cache cache = HttpContext.Current.Cache;
            cache.Insert(key, obj, dependencies, timeForExpired, Cache.NoSlidingExpiration);
        }

        public object GetObject(string key)
        {
            return HttpContext.Current.Cache[key];
        }
        public void Remove(string key)
        {
            HttpContext.Current.Cache.Remove(key);
        }

        protected int CacheExpiredHour
        {
            get
            {
                //int hour=0;
                //int.TryParse(ConfigurationManager.AppSettings["CacheExpiredHour"], out hour);
                return 24;
            }
        }

        #region

        public void InsertByMemoryCache(string key, object obj, CacheItemPolicy policy)
        {
            DateTime timeForExpired = DateTime.Now.AddHours(CacheExpiredHour);
            memoryCache.Add(key, obj, policy);
        }

        public object GetObjectFromMemoryCache(string key)
        {
            return memoryCache.Get(key);
        }

        #endregion


    }
}
